
#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include "QPainter"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //设置窗口大小
    setFixedSize(margin * 2 + block_size * board_size,
                 margin * 2 + block_size * board_size);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    //绘制棋盘
    painter.setRenderHint(QPainter::Antialiasing,true);//抗锯齿
    for(int i=0;i<board_size+1;i++){
        //竖线
        painter.drawLine(margin+block_size*i,margin,
                         margin+block_size*i,size().height()-margin);
        //横线
        painter.drawLine(margin,margin+block_size*i,
                         size().width()-margin,margin+block_size*i);
    }

    //绘制选中点
    QBrush brush;
    brush.setStyle(Qt::SolidPattern);
    //绘制落点标记（防止鼠标出框越界）
    if(clickRow>0 && clickRow<board_size &&
        clickCol>0 && clickCol<board_size &&
        game->gameMapVec[clickRow][clickCol]==0)
    {
        if(game->playerflag)
            brush.setColor(Qt::black);
        else
            brush.setColor(Qt::white);
        painter.setBrush(brush);
        painter.drawRect(margin+block_size*clickCol-mark_size/2,margin+block_size*clickRow-mark_size/2,mark_size,mark_size);
    }
}

void MainWindow::initGame()
{
    //初始化游戏模型
    game=new GameModel;
}
